﻿using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ManagementTool.BaseViewModels
{
    public class ViewModelBase : BindableBase, INavigationAware, IRegionMemberLifetime, IConfirmNavigationRequest
    {
        public IRegionManager Region;

        public IDialogService Dialog;

        public IEventAggregator Event;

        private DelegateCommand _LoadedCommand;

        private DelegateCommand _UnLoadedCommand;

        protected CancellationTokenSource Cts { get; set; }

        protected CancellationToken Token { get; set; }

        protected string PageName { get; set; }

        protected string ItemsName { get; set; }

        public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));

        public DelegateCommand UnLoadedCommand => _UnLoadedCommand ?? (_UnLoadedCommand = new DelegateCommand(ExecuteUnLoadedCommand));

        public bool KeepAlive => false;

        public ViewModelBase(IContainerExtension container)
        {
            Region = container.Resolve<IRegionManager>();
            Dialog = container.Resolve<IDialogService>();
            Event = container.Resolve<IEventAggregator>();
        }

        public virtual void ExecuteUnLoadedCommand()
        {
        }

        public virtual void ExecuteLoadedCommand()
        {
            
        }

        public virtual void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            continuationCallback(obj: true);
        }

        public virtual bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }

        public virtual void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public virtual void OnNavigatedTo(NavigationContext navigationContext)
        {
        }

        protected void OnGarbageCollection()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }
    }
}
